﻿#region <!-- Using Directives -->

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Xml;
using WordSharpDev.Controls;
using WordSharpDev.Extensions;
using WordSharpDev.Properties;
using WordSharpDev.Utils;
using WordSharpDev.Work;
using Office = Microsoft.Office.Core;

using Word = Microsoft.Office.Interop.Word;

#endregion

namespace WordSharpDev
{
	[ComVisible(true)]
	public class WordSharpRibbon : Office.IRibbonExtensibility
	{
		protected Office.IRibbonUI m_Ribbon;

		public WordSharpRibbon()
		{
		}

		public String GetCustomUI(String p_RibbonID)
		{
			return GetResourceText("WordSharpDev.WordSharpRibbon.xml");
		}

		public void Ribbon_Load(Office.IRibbonUI p_Ribbon)
		{
			m_Ribbon = p_Ribbon;
		}

		public Bitmap Image_Load(String p_Name)
		{
			return null;
		}

		#region <!-- GetResourceText -->

		protected static String GetResourceText(String p_Name)
		{
			var l_Assembly = Assembly.GetExecutingAssembly();

			foreach (var l_Resource in l_Assembly.GetManifestResourceNames())
			{
				if (String.Compare(p_Name, l_Resource, StringComparison.OrdinalIgnoreCase) == 0)
				{
					using (var l_Reader = new StreamReader(l_Assembly.GetManifestResourceStream(l_Resource)))
					{
						return l_Reader.ReadToEnd();
					}
				}
			}

			return null;
		}

		#endregion

		#region <!-- GetImage -->

		public Bitmap GetImage(Office.IRibbonControl p_Control)
		{
			if (p_Control.Tag == "ImportData")
				return Properties.Resources.icon_Import_x128;

			if (p_Control.Tag == "ExportData")
				return Properties.Resources.icon_Export_x128;

			if (p_Control.Tag == "VisualStudio")
				return Properties.Resources.icon_VisualStudio_x350;

			if (p_Control.Tag == "Run")
				return Properties.Resources.icon_Run_x096;

			if (p_Control.Tag == "Compile")
				return Properties.Resources.icon_Compile_x240;

			if (p_Control.Tag == "Sample")
				return Properties.Resources.icon_Sample_x150;

			return null;
		}

		#endregion

		#region <!-- OnAction -->

		public void OnAction(Office.IRibbonControl p_Control)
		{
			var l_Exception = default(ErrException);

			var l_EnableEvents = Share.Document.EnableEvents;
			var l_DisplayAlerts = Share.Application.DisplayAlerts;
			var l_ScreenUpdating = Share.Application.ScreenUpdating;

			Share.Document.EnableEvents = false;
			Share.Application.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
			Share.Application.ScreenUpdating = false;

			#region <!-- RunActiveProject -->

			if (p_Control.Id == "RunActiveProject")
			{
				l_Exception = new ErrException("Could not run project");

				try
				{
					Share.UIDialogChoice = 0;

					Share.UIAppPane.SwitchStatusWait();

					Share.UIAppPane.WriteMainLogs("Building project...");

					Share.UIAppPane.WriteDetailLogs("Building project...");

					var l_DocType = Share.Document.Properties.GetXmlString("./@Type");
					var l_DocOutput = Share.Document.Properties.GetXmlString("./@Output");

					if (l_DocType.IsLikeFull("Project") && l_DocOutput.IsLikeFull("Console"))
					{
						Share.Project.CreateNewBuild(Share.Document);

						Share.Build.Generate();

						Share.Build.RunApplication();

						Share.UIAppPane.WriteDetailLogs("Building project... done");

						Share.UIAppPane.WriteMainLogs("Building project... done");

						Share.UIAppPane.SwitchStatusReady();
					}
					else
					{
						throw new NotImplementedException();
					}
				}
				catch (Exception e)
				{
					l_Exception.AppendChildException(e);

					Share.UIAppPane.WriteDetailLogs(Share.FormatException(l_Exception));

					Share.UIAppPane.WriteMainLogs("Building project... error");

					Share.UIAppPane.SwitchStatusError();
				}
			}

			#endregion

			#region <!-- CompileActiveCode -->

			if (p_Control.Id == "CompileActiveCode")
			{
				l_Exception = new ErrException("Could not compile code");

				try
				{
					Share.UIDialogChoice = 0;

					Share.UIAppPane.SwitchStatusWait();

					Share.UIAppPane.WriteMainLogs("Compiling code...");

					Share.UIAppPane.WriteDetailLogs("Compiling code...");

					var l_Target = default(Word.Table);
					var l_Descr = default(XmlNode);

					foreach (Word.Table l_Table in Share.Document.Tables)
					{
						if (Share.Application.Selection.InRange(l_Table.Range))
						{
							l_Target = l_Table;

							l_Descr = l_Table.Descr.AsXmlNode();

							break;
						}
					}

					if (l_Target != null && l_Descr != null && l_Descr.Name.IsLikeFull("Code"))
					{
						var l_CompilePane = new WordSharpBox<CompilePane>();

						var l_CompileData = new Dictionary<String, String>();

						var l_CompileResult = WordCompiler.Compile(l_Target, l_CompileData);

						foreach (var l_Key in l_CompileData.Keys)
						{
							l_CompilePane.Child.AddData(l_Key, l_CompileData[l_Key]);
						}
						
						l_CompilePane.Show(Share.GetAppRect());

						if (!l_CompileResult) throw new Exception();

						Share.UIAppPane.WriteDetailLogs("Compiling code... done");

						Share.UIAppPane.WriteMainLogs("Compiling code... done");

						Share.UIAppPane.SwitchStatusReady();
					}
					else
					{
						throw new Exception("No active code selected");
					}
				}
				catch (Exception e)
				{
					l_Exception.AppendChildException(e);

					Share.UIAppPane.WriteDetailLogs(Share.FormatException(l_Exception));

					Share.UIAppPane.WriteMainLogs("Compiling code... error");

					Share.UIAppPane.SwitchStatusError();
				}
			}

			#endregion

			#region <!-- LoadSample -->

			if (p_Control.Id.IsLikePartial("LoadSample"))
			{
				l_Exception = new ErrException("Could not load sample");

				try
				{
					Share.UIDialogChoice = 0;

					Share.UIAppPane.SwitchStatusWait();

					Share.UIAppPane.WriteMainLogs("Loading sample...");

					Share.UIAppPane.WriteDetailLogs("Loading sample...");

					if (p_Control.Id.IsLikePartial("BasicMaths")) Share.Document.LoadDocument(Resources.GetDocument("SampleBasicMaths"));
				}
				catch (Exception e)
				{
					l_Exception.AppendChildException(e);

					Share.UIAppPane.WriteDetailLogs(Share.FormatException(l_Exception));

					Share.UIAppPane.WriteMainLogs("Loading sample... error");

					Share.UIAppPane.SwitchStatusError();
				}
			}

			#endregion

			Share.Document.EnableEvents = l_EnableEvents;
			Share.Application.DisplayAlerts = l_DisplayAlerts;
			Share.Application.ScreenUpdating = l_ScreenUpdating;
		}

		#endregion
	}
}